using System;

using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Imaging;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.HighLevel.GameEngine2D;
using Sce.PlayStation.HighLevel.GameEngine2D.Base;

using Sce.PlayStation.HighLevel.Physics2D;

namespace UWSGameJame
{
	public class Object
	{
		protected Texture2D texture;
		protected SpriteUV sprite;
		protected TextureInfo ti;
		
		protected bool pickable = false;
		protected bool picked = false;
		
		protected PhysicsShape shape;
		protected PhysicsBody body;
		
		protected float rotation;
								
		public Object (float width, float height)
		{
			texture = new Texture2D("/Application/Resources/Textures/blank.png", true);
  
			ti = new TextureInfo (texture);
   
			sprite = new SpriteUV ();
			sprite.TextureInfo = ti;
   
			sprite.Quad.S = new Vector2(width, height);
			sprite.CenterSprite ();
			
			shape = new PhysicsShape(new Vector2(this.getSize().X, this.getSize().Y));
			
			body = new PhysicsBody(shape, 10.0f);
			body.Position = this.getPosition();
			body.Rotation = this.getRotation();
		}
		
		public Object (String texfilename, float mass)
		{
			try{
				texture = new Texture2D(texfilename, true);
			}
			catch(System.IO.FileNotFoundException)
			{
			}
  
			ti = new TextureInfo(texture);
   
			sprite = new SpriteUV();
			sprite.TextureInfo = ti;
   
			sprite.Quad.S = ti.TextureSizef;
			sprite.CenterSprite ();	
			
			shape = new PhysicsShape(new Vector2(this.getSize().X, this.getSize().Y));
			
			body = new PhysicsBody(shape, mass);
			body.Position = this.getPosition();
			body.Rotation = this.getRotation();
		}
				
		public Object (String texfilename)
		{
			try{
				texture = new Texture2D(texfilename, true);
			}
			catch(System.IO.FileNotFoundException)
			{
			}
  
			ti = new TextureInfo(texture);
   
			sprite = new SpriteUV();
			sprite.TextureInfo = ti;
   
			sprite.Quad.S = ti.TextureSizef;
			sprite.CenterSprite ();	
		}		
		
		public void addToScene(Scene scene)
		{
			scene.AddChild(sprite);
		}
		
		public void setPosition(Vector2 pos)
		{
			sprite.Position = pos;
			
			if(body != null)
				body.Position = pos;
		}
		
		public Vector2 getPosition()
		{
			return sprite.Position;
		}
		
		public void moveRight(float dx)
		{
			sprite.Position = new Vector2(sprite.Position.X + dx, sprite.Position.Y);
			
			if(body != null)
					body.Position = sprite.Position;
		}
		
		public void moveUp(float dy)
		{
			sprite.Position = new Vector2(sprite.Position.X, sprite.Position.Y + dy);
			
			if(body != null)
				body.Position = sprite.Position;
		}
		
		public void move(float dx, float dy)
		{
			sprite.Position = new Vector2(sprite.Position.X + dx, sprite.Position.Y + dy);
			
			if(body != null)
				body.Position = sprite.Position;
		}
		
		public void rotate(float dr)
		{
			sprite.Rotate(Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.Deg2Rad(dr));
			
			if(body != null)
				body.Rotation += Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.Deg2Rad(dr);
			
			this.rotation += dr;
		}
		
		public void setRotation(float rot)
		{
			sprite.Rotation = Vector2.Rotation(Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.Deg2Rad(rot));
			
			if(body != null)
				body.Rotation = sprite.Rotation.X;
			
			this.rotation = rot;
		}
		
		public float getRotation()
		{
			return this.rotation;	
		}
		
		public void scale(float dx, float dy)
		{
			sprite.Quad.S = new Vector2 (sprite.Quad.S.X += dx, sprite.Quad.S.Y += dy);
			sprite.CenterSprite ();
		}
		
		public void setPickable(bool p)
		{
			this.pickable = p;	
		}
		
		public bool isPickable()
		{
			return this.pickable;	
		}
		
		public Vector2 getSize()
		{
		//	Vector2 size = new Vector2(sprite.sithis.texture.Width, this.texture.Height);
			Bounds2 bounds = new Bounds2(new Vector2(0,0));
			
			sprite.GetContentWorldBounds(ref bounds);
			
			Vector2 size = new Vector2((bounds.Max.X - bounds.Min.X) / 2, (bounds.Max.Y - bounds.Min.Y) / 2);
			return size;
		}
		
		public bool isInside(Vector2 pt)
		{
			if (sprite.IsWorldPointInsideContentLocalBounds(pt))
				return true;
			else
				return false;
		}
		
		public bool isPicked()
		{
			return this.picked;	
		}
		
		public void pick()
		{
			if(pickable)
				picked = true;
		}
		
		public void unpick()
		{
			picked = false;	
		}
		
		public SpriteUV getSprite()
		{
			return sprite;
		}
		
		public PhysicsShape getPhysicsShape()
		{
			return this.shape;
		}
		
		public PhysicsBody getPhysicsBody()
		{
			return this.body;
		}
		
		public Vector2 getScale()
		{
			return this.sprite.Quad.S;
		}
		
		public void resetScale()
		{
			sprite.Quad.S = ti.TextureSizef;
			sprite.CenterSprite ();	
		}
	}
}

